package com.dongdongshop.config;

import com.dongdongshop.pojo.TbOperator;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.LoginService;
import com.dongdongshop.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

public class LoginRealm extends AuthorizingRealm {

    @DubboReference
    private UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        UsernamePasswordToken token  = (UsernamePasswordToken) authenticationToken;

        System.out.println(token.getUsername());

        TbUser tb = userService.selectByName(token.getUsername());

        if (tb == null) {
            return null;
        }

        return new SimpleAuthenticationInfo(tb, tb.getPassword(), ByteSource.Util.bytes(tb.getSalt()), tb.getUsername());
    }
}
